//
//  PywCommonSDKApi.m
//  PywCommonSDKDemo
//
//  Created by Ampaw on 2107/8/10.
//  Copyright © 2107年 Ampaw. All rights reserved.
//

#import "PywCommonSDKApi.h"
#import "PywCommonApiManager.h"
#import "PywCommonClientConst.h"
#import "PywCommonPlatformInfo.h"
#import "PywCommonMBProgressHUD+PywCommon.h"
#import "PywCommonSDKParameter.h"
#import "PywCommonMutableDictionary.h"
#import "PywCommonFactoryManager.h"

#import "PywCommonPywLYDataPackHelper.h"
#import "XPCrashLogManager.h"

@implementation PywCommonSDKApi

+ (void)commonSdk_InitWithGamekey:(NSString *)gamekey
{
    // 清楚之前的缓存数据
    [PywCommonFactoryManager clearCache];
    
    [[PywCommonApiManager manager] api_InitWithGamekey:gamekey target:self success:^(NSInteger change_pkg,BOOL use_third_channel, NSDictionary *third_channel_param, typeof(self) target) {
        
        // 存储是否切换渠道信息
        [PywCommonPlatformInfo sharedCommonPlatformInfo].use_third_channel = use_third_channel;
        [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg = change_pkg;
        [[self getPlugin:change_pkg] channelApi_initWithParam:third_channel_param];
    }];
    
    // 注册监听异常上报
    [[XPCrashLogManager manager] InstallUncaughtExceptionHandlerWithIsOpenDebug:NO];
}

/**
 登录
 */
+ (void)commonSdk_ShowLogin
{
    if ([PywCommonPlatformInfo sharedCommonPlatformInfo].isInitSuccess == false) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                            message:@"Server exception, initialization request failed !"
                                                           delegate:nil
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:@"OK", nil];
        [alertView show];
        return;
    }
    
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    [[self getPlugin:change_pkg] channelApi_ShowLogin];
}

/**
 注销
 */
+ (void)commonSdk_ShowLogout
{
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    [[self getPlugin:change_pkg] channelApi_ShowLogout];
}

/**
 支付
 
 @param payInfo 支付信息
 当支付信息中，包含ProductId时，使用的“定额支付”
 当支付信息中，未含ProductId时，使用的“任意支付”
 */
+ (void)commonSdk_StartPay:(NSDictionary *)payInfo
{
    // 当聚合初始化返回，不使用三方渠道时，整个流程走母包朋友玩端，不必执行聚合下单接口【即：使用了朋友玩的渠道登录】
    if ([PywCommonPlatformInfo sharedCommonPlatformInfo].use_third_channel &&
        ![PywCommonSDKApi isOwnChannel:[PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg]) {
        // 聚合创建订单
        [[PywCommonApiManager manager] api_CreateOrdertwithPayInfo:payInfo target:self success:^(NSInteger change_pkg, BOOL use_third_channel,  NSDictionary *third_channel_param, id target) {
            // 渠道支付,判断是否使用三方渠道
            if (use_third_channel) {
                NSInteger cid = [PywCommonFactoryManager channelCode];
                if (cid == change_pkg) {
                    [[PywCommonFactoryManager channelManager] channelApi_StartPay:payInfo];
                }else {
                    // 17不是指渠道号,是指使用朋友玩（即优趣,疯趣）等,17相当于是没有聚合三方渠道，走母包朋友玩整个流程，无需再走聚合接口
                    [[PywCommonFactoryManager pywSDKIndiePayManager] channelApi_StartPay:[PywCommonPywLYDataPackHelper packPywLYPayInfo:payInfo paytype:CommonPayType_Indie]];
                }
            }else{
                [[PywCommonFactoryManager pywSDKIndiePayManager] channelApi_StartPay:[PywCommonPywLYDataPackHelper packPywLYPayInfo:payInfo paytype:CommonPayType_Indie]];
            }
        }];
    }else{
        // 使用默认母包的整个流程支付
        [[PywCommonFactoryManager pywLYManager] channelApi_StartPay:[PywCommonPywLYDataPackHelper packPywLYPayInfo:payInfo paytype:CommonPayType_Normal]];
    }
}

+(BOOL) isOwnChannel:(NSInteger) chid{
    return chid==0||chid==21||chid==23||chid==24;
}

/**
 提交玩家数据
 
 @param playerInfo 玩家数据
 */
+ (void)commonSdk_SetPlayerInfo:(NSDictionary *)playerInfo
{
//    [[PywCommonFactoryManager pywLYManager] channelApi_SetPlayerInfo:playerInfo];
    // 首先，同步数据到“朋友玩”
    [[PywCommonApiManager manager] api_UploadRoleInfo:playerInfo];
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    [[self getPlugin:change_pkg] channelApi_SetPlayerInfo:playerInfo];
}

/**
 设置悬浮穿的显示与隐藏
 
 @param status 当status = YES时，显示悬浮窗；当status = NO时，不显示悬浮窗；
 */
+ (void)commonSdk_ShowOrHideFloatView:(BOOL)status
{
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    [[self getPlugin:change_pkg] channelApi_ShowOrHideFloatView:status];
}

/**
 显示 用户中心
 */
+ (void)commonSdk_ShowUserCenter
{
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    [[self getPlugin:change_pkg] channelApi_ShowUserCenter];
}

/**
 返回 App 参数传递
 
 @param url URL Schemes
 */
+ (void)commonSdk_OpenUrl:(NSURL *)url
{
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    [[self getPlugin:change_pkg] channelApi_OpenUrl:url];
}

/**
 切换应用运行模式
 
 @param isForeground 当isForeground = YES, 应用前台运行；当isForeground = NO, 应用后台运行或退出程序；
 @param application 当前应用
 */
+ (void)commonSdk_IsWillEnterForeground:(BOOL)isForeground OtherWiseEnterBackgroundOrExitApplication:(UIApplication *)application
{
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    [[self getPlugin:change_pkg] channelApi_IsWillEnterForeground:isForeground OtherWiseEnterBackgroundOrExitApplication:application];
}

/**
 获取本地渠道的配置信息
 
 @return 配置信息
 */
+ (NSString *)commonSdk_GetPlatformData
{
    // 调用对应渠道SDK接口
    NSInteger change_pkg = [PywCommonPlatformInfo sharedCommonPlatformInfo].change_pkg;
    return [[self getPlugin:change_pkg] channelApi_GetPlatformData];
}



/**
 *根据渠道号返回相应的插件类
 **/
+(id) getPlugin:(NSInteger)change_pkg{
    if (![PywCommonPlatformInfo sharedCommonPlatformInfo].use_third_channel) {
        return [PywCommonFactoryManager pywLYManager];
    }
    NSInteger cid = [PywCommonFactoryManager channelCode];
    if (cid == change_pkg) {
        return [PywCommonFactoryManager channelManager];
    }else {
        // 17不是指渠道号,是指使用朋友玩（即优趣,疯趣）等,17相当于是没有聚合三方渠道，走母包朋友玩整个流程，无需再走聚合接口
        return [PywCommonFactoryManager pywLYManager];
    }
}



@end
